<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="1080">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="180px"
      v-loading="formLoading"
    >
      <!-- <el-col :span="8">
          <el-form-item label="供应商" prop="supplierId">
            <el-select
              v-model="formData.supplierId"
              clearable
              filterable
              placeholder="请选择供应商"
              class="!w-1/1"
            >
              <el-option
                v-for="item in supplierList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col> -->
    </el-form>
    <!-- 子表的表单 -->
    <el-tabs v-model="subTabsName">
      <el-tab-pane label="供应商列表" name="product">
        <ProductForm ref="productFormRef" :id="formData.id"/>
      </el-tab-pane>

    </el-tabs>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { RelationshipApi, RelationshipVO } from '@/api/spd/relationship'
import ProductForm from './components/ProductForm.vue'
import { ProductApi, ProductVO } from '@/api/spd/product/product'
import { List } from 'echarts'
/** 物料供应商关联 表单 */
defineOptions({ name: 'RelationshipForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const dataList = ref<ProductVO[]>([])
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  productId: undefined,
  supplierIds: undefined,
  sort:undefined,
  minOrderQuantity:undefined
  
})

const formRef = ref() // 表单 Ref

/** 子表的表单 */
const subTabsName = ref('product')
const productFormRef = ref('id')

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t("挂接")
  formType.value = type

  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ProductApi.getQuantity(id)
      formData.value.productId = formData.value.id
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗


/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  let productFormData = null;
  // 校验子表单
  try {
     productFormData =await productFormRef.value.getData()
  } catch (e) {
    subTabsName.value = 'product'
    return
  }

  // 提交请求
  formLoading.value = true
  try {
    formData.value.supplierIds = productFormData
    // formData.value.sort = productFormData.value.minOrderQuantity
    console.log("supplierIds",productFormData)
    const data = formData.value as unknown as RelationshipVO
      await RelationshipApi.createRelationship(data)
      message.success(t('common.connectionSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    productId: undefined,
    supplierIds: undefined,
  }
  formRef.value?.resetFields()
}
</script>